package com.xiaozhen.common.core.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日期工具类
 *
 * @author haigle
 * @date 2018/11/23 17:11
 */
public class DateUtils {

    /**
     * yyyy-MM-dd
     */
    public static final String YMD1 = "yyyy-MM-dd";

    /**
     * yyyy/MM/dd
     */
    public static final String YMD2 = "yyyy/MM/dd";

    /**
     * yyyy.MM.dd
     */
    public static final String YMD3 = "yyyy.MM.dd";

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String YMDHMS1 = "yyyy-MM-dd HH:mm:ss";

    /**
     * yyyy/MM/dd HH:mm:ss
     */
    public static final String YMDHMS2 = "yyyy/MM/dd HH:mm:ss";

    /**
     * yyyy.MM.dd HH:mm:ss
     */
    public static final String YMDHMS3 = "yyyy.MM.dd HH:mm:ss";

    /**
     * yyyy-MM-dd HH:mm
     */
    public static final String YMDHM1 = "yyyy-MM-dd HH:mm";

    /**
     * yyyy/MM/dd HH:mm
     */
    public static final String YMDHM2 = "yyyy/MM/dd HH:mm";

    /**
     * yyyy.MM.dd HH:mm
     */
    public static final String YMDHM3 = "yyyy.MM.dd HH:mm";

    /**
     * yyyy-MM
     */
    public static final String YM1 = "yyyy-MM";

    /**
     * yyyy/MM
     */
    public static final String YM2 = "yyyy/MM";

    /**
     * yyyy.MM
     */
    public static final String YM3 = "yyyy.MM";

    private DateUtils() {
    }

    public static LocalDateTime parseStringToLocalDateTime(String timeString, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(timeString, df);
    }

    public static String getLocalDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    /**
     * 获取毫秒， 网上百度都有
     */
    public static Long getmicTime() {
        // 微秒
        Long cutime = System.currentTimeMillis() * 1000;
        // 纳秒
        Long nanoTime = System.nanoTime();
        return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
    }
}
